import { mapState } from 'vuex'
export default {
    computed: {
        ...mapState('admin/layout', ['currentAccount'])
    },
    methods: {
        // 审核
        async review (row, index) {
            let _this = this;
            this.$Modal.confirm({
                title: this.lang['confirmReview'],
                cancelText: this.lang['cancel'],
                async onOk () {
                    _this.changeReview(row, 'review', _this)
                }
            })
        },
        // 反审
        reverseReview (row, index) {
            let _this = this;
            this.$Modal.confirm({
                title: this.lang['confirmReverseReview'],
                cancelText: this.lang['cancel'],
                async onOk () {
                    _this.changeReview(row, 'reverseReview', _this)
                }
            })
        },
        // 修改状态
        async changeReview (row, reviewFlag, _this) {
            if (await _this.getRecord({ type: row.type, className: row.className, id: row.id })) {
                let data = _this.classObject.record
                if (reviewFlag === 'review') {
                    if (this.isSamePerson()) {
                        return;
                    }
                    _this.$set(_this.classObject.record.values, this.getReviewStrInfo(this.view).checkPersonStr, [{ personId: this.currentAccount.account, personName: this.currentAccount.userName }])
                    _this.$set(_this.classObject.record.values, this.getReviewStrInfo(this.view).checkTimeStr, this.tools.formatDate(new Date(), 'DateTime'))
                    data.values[this.getReviewStrInfo(this.view).checkStatus] = '审批通过'
                } else if (reviewFlag === 'reverseReview') {
                    if (this.isReverseReviewValid()) {
                        return;
                    }
                    _this.$set(_this.classObject.record.values, this.getReviewStrInfo(this.view).checkPersonStr, '')
                    _this.$set(_this.classObject.record.values, this.getReviewStrInfo(this.view).checkTimeStr, '')
                    data.values[this.getReviewStrInfo(this.view).checkStatus] = '未审批'
                }
                _this.loading = false;
                _this.classObject.setRecord(data);
                _this.classObject.update().then((flag) => {
                    if (flag) {
                        let index = _this.collectionObject.records.findIndex(
                            (item) => item.id === _this.classObject.record.id
                        );
                        if (index !== -1) {
                            _this.collectionObject.records.splice(
                                index,
                                1,
                                _this.classObject.record
                            );
                        }
                    }
                });
            } else {
                _this.loading = false;
            }
        },
        // 审核：当前账号与创建人不能为同一人
        isSamePerson () {
            if (this.currentAccount.account === this.classObject.record.createdBy) {
                    Message.error('审核人与创建人不能为同一人')
                    return true;
            } else {
                return false;
            }
        },
        /**
         * 根据页面标识，获取对应的审核字段
         */
        getReviewStrInfo (viewFlag) {
            switch (viewFlag) {
            case 'energyEstimationOrder': // 能源暂估登记
                return {
                    checkPersonStr: 'checker', // 审核人字段名称
                    checkTimeStr: 'checkTime', // 审核时间字段名称
                    checkStatus: 'checkStatus' // 审核状态字段名称
                };
            default:
                return {
                    checkPersonStr: 'checker', // 审核人字段名称
                    checkTimeStr: 'checkTime', // 审核时间字段名称
                    checkStatus: 'checkStatus', // 审核状态字段名称
                }
            }
        }
    }
}
